package com.hy.appium3;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class AppiumDemo1HyBrid {

    AndroidDriver driver;


    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.handsgo.jiakao.android");
        desiredCapabilities.setCapability("automationName", "uiautomator2");

        //默认启动不初始化软件
        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.handsgo.jiakao.android.splash.Login");
        //创建驱动
//        AndroidDriver<WebElement> Driver = new AndroidDriver<WebElement>(url, desiredCapabilities);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @Test
    public void test() throws InterruptedException {

        String AssertDialog = "com.handsgo.jiakao.android:id/closeIv";
        if (byElement(By.id(AssertDialog)) == true) {
            driver.findElementById(AssertDialog).click();
        }
        else {
            System.out.println("n");
        }


        // vip课程   com.handsgo.jiakao.android:id/practice_button_1

        driver.findElementById("com.handsgo.jiakao.android:id/practice_button_1").click();

        Thread.sleep(10000);

        //获取所有视图

        Set contextHandles = driver.getContextHandles();
        System.out.println(contextHandles);

        Thread.sleep(10000);

        driver.context("WEBVIEW_com.handsgo.jiakao.android");
        Thread.sleep(5000);


        //点击html元素
        driver.findElementByXPath("/html/body/div/div[3]/div/div[1]/div[2]").click();
        Thread.sleep(10000);




    }

    @AfterMethod
    public void TestDown() {
//        Driver.quit();
    }

    public boolean byElement(By btId) {
        try {
            driver.findElement(btId);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}
